package lock.method;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class MyService2 {
    public ReentrantLock lock = new ReentrantLock();

    public void waitMethod() {
        try {
            // System.out.println(lock.tryLock(3, TimeUnit.SECONDS));
            if(lock.tryLock(3, TimeUnit.SECONDS)) {
                System.out.println("    " + Thread.currentThread().getName() +
                    "获得所的时间" + System.currentTimeMillis());
                Thread.sleep(100000);
                System.out.println("*************");
            } else {
                System.out.println("    " + Thread.currentThread().getName() +
                        "没有获得所");
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if(lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
}
